package com.mt.operation.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.operation.entity.ArticleEntity;
import com.mt.operation.mapper.ArticleEntityMapper;

@Component
public class ArticleServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ArticleServiceModel.class);

    @Autowired
    private ArticleEntityMapper articleEntityMapper;

    public ServiceResult<List<ArticleEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.articleEntityMapper, "Property 'articleEntityMapper' is required.");
        ServiceResult<List<ArticleEntity>> result = new ServiceResult<List<ArticleEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }

            result.setResult(this.articleEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ArticleServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<ArticleEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.articleEntityMapper, "Property 'articleEntityMapper' is required.");
        ServiceResult<ArticleEntity> result = new ServiceResult<ArticleEntity>();
        try {
            result.setResult(this.articleEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ArticleServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> insert(ArticleEntity articleEntity) {
        Assert.notNull(this.articleEntityMapper, "Property 'articleEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.articleEntityMapper.insert(articleEntity);
            if (save > 0) {
                id = articleEntity.getArticleId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ArticleServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateRelate(ArticleEntity articleEntity) {
        Assert.notNull(this.articleEntityMapper, "Property 'articleEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.articleEntityMapper.updateByPrimaryKeySelective(articleEntity);
            if (save > 0) {
                id = articleEntity.getArticleId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ArticleServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(ArticleEntity articleEntity) {
        Assert.notNull(this.articleEntityMapper, "Property 'articleEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.articleEntityMapper.updateByPrimaryKeySelective(articleEntity);
            if (save > 0) {
                id = articleEntity.getArticleId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ArticleServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer articleId) {
        Assert.notNull(this.articleEntityMapper, "Property 'articleEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int save = 0;
        try {
            save = this.articleEntityMapper.deleteByPrimaryKey(articleId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ArticleServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }
}
